<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/UserCom.php");

$usercom = new UserCom();

$cont = $_REQUEST['cont'];
switch ($cont) {
    case 1://获取用户信息
        $id = $_REQUEST['id'];
        $$userinfo = $usercom->getUserInfo($id);
        $resdata = array();
        if ($userinfo) {
            $resdata['name'] = $userinfo['name'];
            $resdata['sex'] = $userinfo['sex'];
            $resdata['address'] = $userinfo['address'];
            $resdata['mobile'] = $userinfo['mobile'];
            echo json_encode(array("code" => 0, "msg" => "success", "data" => $resdata));
        }else{
            echo json_encode(array("code" => 400, "msg" => "get userinfo fail"));
        }
        break;
    case 2://保存
        $name = $_REQUEST['name'];
        $mobile = $_REQUEST['mobile'];
        $address = $_REQUEST['address'];
        $sex = $_REQUEST['sex'];
        $id = $_REQUEST['id'];
        $res = $usercom->saveUserInfo($id, $name, $adddress, $sex, $mobile);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "save userinfo fail"));
        }
        break;
    case 3://删除
        $id = $_REQUEST['id'];
        $res = $usercom->deleteUser($id);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "delete userinfo fail"));
        }
        break;
    case 4://批量删除
        $ids = $_REQUEST['ids'];
        $idarr = explode(",", $ids);
        foreach($idarr as $v){
            $res = $usercom->deleteUser($v);
            if(!$res){
                echo json_encode(array("code" => 400, "msg" => "delete userinfo fail"));
                exit();
            }
        }
        echo json_encode(array("code" => 0, "msg" => "success"));
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined fail"));
        break;
}
exit();
